$(function () {
	Vue.http.options.emulateJSON = true;
	
	$("input[name='attDate']").datetimepicker({
	    format: 'yyyy-mm-dd hh:ii:ss',
	    minView: 0,
	    autoclose: true,
	    language: "zh-CN"
	});
	
	// 分页参数条件
	function queryParams() {
		var param = {
			"name" : $("#student-name").val(),
			"classes.name" : null,
			"classes.product.id" : null,
			pageNum : this.pageNumber,
			pageSize : this.pageSize
		}
		return param;
	}
	
	// 响应消息处理
	function responseHandler(res) {
		if (res) {
			return {
				"rows" : res.rows,
				"total" : res.total
			};
		} else {
			return {
				"rows" : [],
				"total" : 0
			};
		}
	}

	function operateFormatter(value, row, index) {
		return [ '<a class="select" href="javascript:void(0)" title="选择">',
				'<i class="glyphicon glyphicon-check">选择</i>', '</a>  ' ]
				.join('');
	}

	var operateEvents = {
		'click .select' : function(e, value, row, index) {
			$('input[name="studentName"]').val(row.name);
			$('input[name="studentId"]').val(row.id);
			$("#studentSelect").modal("hide");
		}
	};
	
	$("#studentForm").bootstrapTable({
		method : "get",
		dataType : "json",
		contentType : "application/json",
		cache : false,
		striped : true, // 是否显示行间隔色
		sidePagination : "server", // 服务端分页
		pagination : true,
		url : "student/lists",
		queryParams : queryParams,
		pageNumber : 1, // 初始化加载第一页，默认第一页
		pageSize : 10, // 每页的记录行数（*）
		pageList : [ 10, 25, 50, 100 ], // 可供选择的每页的行数（*）
		showExport : false,
		responseHandler : responseHandler,
		columns : [ {
			field : "id",
			title : "编号",
			align : "center",
			valign : "middle"
		}, {
			field : "name",
			title : "姓名",
			align : "center",
			valign : "middle"
		}, {
			field : "tel",
			title : "手机号码",
			align : "center",
			valign : "middle"
		}, {
			field : "classes.name",
			title : "班级",
			align : "center",
			valign : "middle"
		}, {
			field : "jobIntention",
			title : "工作意向",
			align : "center",
			valign : "middle"
		}, {
			title : "操作",
			align : "center",
			valign : "middle",
			events : operateEvents,
			formatter : operateFormatter
		} ]
	});
	
	$("button.selectStudent").click(function() {
		$("#studentForm").bootstrapTable("refresh");
	});
	
	$("input[type='file']").change(function () {
		var val = $(this).val();
		
		if (val !== "") {
			$("input[name='file-text']").val(val);			
		}
	});
	
	$("input[name='file-text']").click(function () {
		$("input[type='file']").click();
	});
	
	$(".remedy").validate({
		errorElement : "label",  
        errorClass : "text-danger",  
        rules : {
        	studentName : {  
            	required : true,
            	minlength: 2,
            	maxlength: 50
            },
            attDate : {
            	required : true,
            	minlength: 19,
            	maxlength: 19
            }
        },
        submitHandler: function(form) {
        	$.showloading();
        	Vue.http.post(
				"attendance/remedy", 
				{
					'student.id' : $('input[name="studentId"]').val(),
					attDate : $('input[name="attDate"]').val()
				}
			).then((res) => {
				var data = res.data; 
				console.log(data);
				
				bootbox.alert({
				    message: data.msg,
				    size: 'small'
				});
			}, (res) => {
				bootbox.alert({
				    message: "系统错误",
				    size: 'small'
				});
			}).finally(function() {
				$.hideloading();
				$('input[name="studentName"]').val('');
				$('input[name="attDate"]').val('');
			});
		}
	});
	
	$(".import").validate({
		errorElement : "label",  
        errorClass : "text-danger",  
        rules : {
        	'file-text': {  
            	required : true
            }
        },
        submitHandler: function(form) {
        	$.showloading();
        	var formData = new FormData();
        	formData.append('file', $('input[name="file"]').get(0).files[0]);
        	Vue.http.post(
				"attendance/import",
				formData
			).then((res) => {
				var data = res.data; 
				console.log(data);
				
				bootbox.alert({
				    message: data.msg,
				    size: 'small'
				});
			}, (res) => {
				bootbox.alert({
				    message: "系统错误",
				    size: 'small'
				});
				
			}).finally(function() {
				
				$.hideloading();
				$('input[name="file-text"]').val('');
				$('input[name="file"]').val('');
			});
		}
	});
});